﻿#include "BrowserTableHandlerSubsystemFEAcoustic.h"
#include "OperatorsInterface/OperAbstractSubsystem.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractFESubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKFESubSysAcoustic.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"
#include "FITK_Component/FITKAcousticsModel/FITKFESubSysGeoProp.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include <QCheckBox>

namespace GUI
{
    void BrowserTableHandlerSubsystemFEAcoustic::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Fluid") << tr("Damping")
            << tr("Number of Elements") << tr("Volume[m3]") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerSubsystemFEAcoustic::updateData()
    {
        if (_tableWidget == nullptr) return;
        if (_feMgr == nullptr || _specMgr == nullptr)return;

        QList<Acoustics::FITKAbstractFESubSys*> list = _feMgr->getSubSysByType(
            Acoustics::FITKAbstractFESubSys::FEAcoustic);
        //FE腔数量
        const int n = list.size();
        _tableWidget->setRowCount(n);
        for (int i = 0; i < n; ++i)
        {
            //转换为SEA声腔
            Acoustics::FITKFESubSysAcoustic* fe = dynamic_cast<Acoustics::FITKFESubSysAcoustic*>(list.at(i));
            if (fe == nullptr) continue;

            //数据ID
            const int id = fe->getDataObjectID();

            _tableWidget->setItem(i, 0, new QTableWidgetItem(fe->getDataObjectName(), id));
          
            if (fe->getAllElementPropName().size() <= 0)continue;
            QString fluidName=fe->getAllElementPropName()[0];
            _tableWidget->setItem(i, 1, new QTableWidgetItem(fluidName, id));

            QString specName = "";
            if (fe->getDampingSpectraObj())specName = fe->getDampingSpectraObj()->getDataObjectName();
            _tableWidget->setItem(i, 2, new QTableWidgetItem(specName, id));

            QString eleNum = QString::number(fe->getElementsID().size());
            _tableWidget->setItem(i, 3, new QTableWidgetItem(eleNum, id));

            if(!fe->getGeoProp())continue;
            double volume = fe->getGeoProp()->getGeoProp(Acoustics::FITKFESubSysGeoProp::Volume);     
            _tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(volume), id));

            _tableWidget->setItem(i, 5, new QTableWidgetItem(fe->getNote(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractSubsystem * BrowserTableHandlerSubsystemFEAcoustic::getSubsystemOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractSubsystem>("OperSubsystemFEAcoustic");
    }

    bool BrowserTableHandlerSubsystemFEAcoustic::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //流体属性 频谱
        if (colIndex <1|| colIndex> 2) return false;

        return true;
    }
}